package com.colin.demo.web;

import com.colin.utils.txt.TxtFileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;

/**
 * Created by Administrator on 2017/8/9 0009.
 */
@Controller
public class DemoSpringBoot {

    @RequestMapping(value = "/")
    @ResponseBody
    public String index() throws Exception {
        InputStream stream = getClass().getClassLoader().getResourceAsStream("static/demo.xls");
        File targetFile = new File("c:/11.xls");
        try {
            FileCopyUtils.copy(stream, new FileOutputStream(targetFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        InputStream source = getClass().getClassLoader().getResourceAsStream("static/source.json");
        File sourceFile=new File("copySource.json");
        inputstreamtoFile(source,sourceFile);
        String con=TxtFileUtils.readTxtFile(sourceFile);
        System.out.println("================");
        System.out.println(con);
        System.out.println("================");

        return "";
    }


    public void inputstreamtoFile(InputStream ins, File file) throws Exception {
        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }
}
